package com.maimao.model.info;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.lang.annotation.Documented;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 优惠券Mongo实体
 *
 * @author MaoLin Wang
 * @date 2021/1/7 7:14 下午
 */
@Document(collection = "mm_coupon")
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class MgCoupon implements Serializable {


    private static final long serialVersionUID = -7958911985315001704L;
    @Id
    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;

    /**
     * 1 通用券 用户都可以领 2 新人领取(注册后系统赠送) 3 首单优惠 4 指定用户（vip等） 『目前只支持1和2』
     */
    @NotNull(message = "请选择使用群体")
    private Integer who;

    /**
     * 1 用户自己领取 2 系统发放
     */
    @NotNull(message = "请选择领取方式")
    private Integer how;

    /**
     * 范围：1. 单品优惠 2.指定商品 3.全店铺使用 4.专区 5. 全场    『1、2、3用于店铺优惠券，4和5用于系统优惠券』
     */
    @Indexed
    @NotNull(message = "请选择适用范围")
    private Integer where;

    /**
     * 1 满减 2 立减
     */
    @NotNull(message = "请选择减免方式")
    private Integer what;

    /**
     * 1 普通(规定时间范围内可用) 2 限时(如领取后几日内可用)
     */
    @NotNull(message = "请选择期限类型")
    private Integer useType;

    /**
     * 1 店铺优惠 2 系统优惠
     */
    @NotNull(message = "请选择优惠券类型")
    private Integer type;

    @NotBlank(message = "请输入优惠券名称")
    private String name;

    private String desc;

    /**
     * mongo时区相差8小时 使用GMT+8对结果进行转换 查询日期的时候也会按照+8小时之后的结果进行查询
     */
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss",iso = DateTimeFormat.ISO.DATE_TIME)
    @NotNull(message = "请输入开始时间")
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime startTime;

    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(shape = JsonFormat.Shape.STRING, timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss", iso = DateTimeFormat.ISO.DATE_TIME)
    @NotNull(message = "请输入结束时间")
    private LocalDateTime endTime;

    /**
     * 最低消费(进行满减)
     */
    private BigDecimal min;

    /**
     * 优惠金额 what为1 需满max_price，what为0 ，直接减
     */
    @NotNull(message = "请输入优惠金额")
    private BigDecimal discountPrice;

    @NotNull(message = "请输入发行量")
    @Min(1)
    private Integer count;

    /**
     * 使用数量
     */
    private Integer used;

    /**
     * 领取数量
     */
    private Integer takeCount;

    /**
     * 余量 count-takeCount
     */
    @Transient
    private Integer stock;

    /**
     * 针对限时优惠券规定领取后使用期限(天)
     */
    private Integer limit;

    /**
     * json列表 where单品优惠和指定商品的目标商品id列表
     */
    private List<String> goodsId;

    /**
     * where为3(专区时)优惠券的适用专区id 暂未实现
     */
    private Integer channelId;

    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss", shape = JsonFormat.Shape.STRING)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;

    private Integer deleted;

    @Indexed
    @JsonSerialize(using = ToStringSerializer.class)
    private Long shopId;

    /**
     * 当前登录用户是否领取过 领取过则为false不允许领取 未登录则为true
     */
    @Transient
    private Boolean can = true;

    @Override
    public String toString() {
        return "MgCoupon{" +
                "id=" + id +
                ", who=" + who +
                ", how=" + how +
                ", where=" + where +
                ", what=" + what +
                ", stock=" + stock +
                ", useType=" + useType +
                ", type=" + type +
                ", name='" + name + '\'' +
                ", desc='" + desc + '\'' +
                ", startTime=" + startTime +
                ", endTime=" + endTime +
                ", min=" + min +
                ", discountPrice=" + discountPrice +
                ", count=" + count +
                ", used=" + used +
                ", takeCount=" + takeCount +
                ", limit=" + limit +
                ", goodsId=" + goodsId +
                ", channelId=" + channelId +
                ", createTime=" + createTime +
                ", deleted=" + deleted +
                ", shopId=" + shopId +
                '}';
    }
}
